package org.db.ui;

import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JDialog;
import javax.swing.JOptionPane;

import org.db.Manager;

public class CloseCommitDialog extends JDialog implements WindowListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7069985993859000576L;

	protected int returnValue;

	public CloseCommitDialog(JDialog owner) {
		super(owner);
		addWindowListener(this);
		setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
		setLocationRelativeTo(null);
	}

	/**
	 * Programmierung der Abbruchsbestätigung
	 */
	protected void askForClosing() {
		int ret = JOptionPane
				.showConfirmDialog(
						this,
						"Wollen Sie abbrechen und alles bisherigen Eingaben verwerfen?",
						"Abbruch", JOptionPane.YES_NO_OPTION);
		if (ret == JOptionPane.YES_OPTION) {
			returnValue = JOptionPane.CANCEL_OPTION;
			Manager.getInstance().cancel();
			close();
		}
	}

	public int showDialog() {
		return returnValue;
	}

	protected void close() {
		setVisible(false);
		dispose();
	}

	@Override
	public void windowActivated(WindowEvent e) {

	}

	@Override
	public void windowClosed(WindowEvent e) {

	}

	@Override
	public void windowClosing(WindowEvent e) {
		askForClosing();
	}

	@Override
	public void windowDeactivated(WindowEvent e) {

	}

	@Override
	public void windowDeiconified(WindowEvent e) {

	}

	@Override
	public void windowIconified(WindowEvent e) {

	}

	@Override
	public void windowOpened(WindowEvent e) {

	}

}
